package com.UnionPayCenter.util;

import com.UnionPayCenter.entity.ShopInfoEntity;
import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.ClassUtils;

import java.io.*;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

/**
 * @program: UnionPayServers
 * @description:
 * @author: bite
 * @create: 2019-06-10 11:49
 **/
@Slf4j
public class FileUtil {
    /** 
    * @Description: 删除文件夹
    * @Param:  
    * @return:  
    * @Author: bite 
    * @Date: 2019/6/10 
    */
    public static void deleteDir(String dirPath) {
        File file = new File(dirPath);
        if(file.isFile()) {
            file.delete();
        }else {
            File[] files = file.listFiles();
            if(files == null) {
                file.delete();
            }else {
                for (int i = 0; i < files.length; i++) {
                    deleteDir(files[i].getAbsolutePath());
                }
                file.delete();
            }
        }
    }

    /**
    * @Description: 复制文件夹 
    * @Param:  
    * @return:  
    * @Author: bite 
    * @Date: 2019/6/10 
    */
    public static void copyDir(String sourcePath, String newPath) throws IOException {
        File file = new File(sourcePath);
        String[] filePath = file.list();

        if (!(new File(newPath)).exists()) {
            (new File(newPath)).mkdirs();
        }

        for (int i = 0; i < filePath.length; i++) {
            if(!"project.config.json".equals(filePath[i])){
                if ((new File(sourcePath + file.separator + filePath[i])).isDirectory()) {
                    copyDir(sourcePath  + file.separator  + filePath[i], newPath  + file.separator + filePath[i]);
                }

                if (new File(sourcePath  + file.separator + filePath[i]).isFile()) {
                    copyFile(sourcePath + file.separator + filePath[i], newPath + file.separator + filePath[i]);
                }
            }
        }
    }

    /** 
    * @Description:  复制文件的方法
    * @Param:  
    * @return:  
    * @Author: bite 
    * @Date: 2019/6/10 
    */ 
    public static void copyFile(String oldPath, String newPath) throws IOException {
        File oldFile = new File(oldPath);
        File file = new File(newPath);
        FileInputStream in = new FileInputStream(oldFile);
        FileOutputStream out = new FileOutputStream(file);;

        byte[] buffer=new byte[2097152];
        int readByte = 0;
        while((readByte = in.read(buffer)) != -1){
            out.write(buffer, 0, readByte);
        }

        in.close();
        out.close();
    }

    /**
     * 写入文件，覆盖原内容
     * @param content
     * @param path
     * @return
     * @throws Exception
     */
    public static void writeFile(String content, String path)throws Exception{
        File fileName = new File(path);
        FileOutputStream fileOutputStream = new FileOutputStream(fileName);
        fileOutputStream.write(content.getBytes("utf-8"));
        fileOutputStream.close();
    }

}
